﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace TimKen.DataAccess
{
   public   class ShoppingCartDAO
   {
        /// <summary>
        /// 数据访问类ShoppingCart。
        /// </summary>
     
            public ShoppingCartDAO()
            { }
            #region  成员方法

            /// <summary>
            /// 得到最大ID
            /// </summary>
            public int GetMaxId()
            {
                return DbHelperSQL.GetMaxID("ProductID", "ShoppingCart");
            }

            /// <summary>
            /// 是否存在该记录
            /// </summary>
            public bool Exists(string UserName, int ProductID)
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@ProductID", SqlDbType.Int,4)};
                parameters[0].Value = UserName;
                parameters[1].Value = ProductID;

                int result = DbHelperSQL.RunProcedure("UP_ShoppingCart_Exists", parameters, out rowsAffected);
                if (result == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            /// <summary>
            ///  增加一条数据
            /// </summary>
            public void Add(TimKen.DataEntity.ShoppingCart model)
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@Number", SqlDbType.Float,8),
					new SqlParameter("@DateAdded", SqlDbType.DateTime)};
                parameters[0].Value = model.UserName;
                parameters[1].Value = model.ProductID;
                parameters[2].Value = model.Number;
                parameters[3].Value = model.DateAdded;

                DbHelperSQL.RunProcedure("UP_ShoppingCart_ADD", parameters, out rowsAffected);
            }

            /// <summary>
            ///  更新一条数据
            /// </summary>
            public void Update(TimKen.DataEntity.ShoppingCart model)
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@Number", SqlDbType.Float,8),
					new SqlParameter("@DateAdded", SqlDbType.DateTime)};
                parameters[0].Value = model.UserName;
                parameters[1].Value = model.ProductID;
                parameters[2].Value = model.Number;
                parameters[3].Value = model.DateAdded;

                DbHelperSQL.RunProcedure("UP_ShoppingCart_Update", parameters, out rowsAffected);
            }

            /// <summary>
            /// 删除一条数据
            /// </summary>
            public void Delete(string UserName, int ProductID)
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@ProductID", SqlDbType.Int,4)};
                parameters[0].Value = UserName;
                parameters[1].Value = ProductID;

                DbHelperSQL.RunProcedure("UP_ShoppingCart_Delete", parameters, out rowsAffected);
            }

            /// <summary>
            /// 清空购物车
            /// </summary>
            public void CleanCart(string UserName)
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
                parameters[0].Value = UserName;

                DbHelperSQL.RunProcedure("UP_ShoppingCart_Clean", parameters, out rowsAffected);
            }
            /// <summary>
            /// 得到一个对象实体
            /// </summary>
            public TimKen.DataEntity.ShoppingCart GetModel(string UserName, int ProductID)
            {
                SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@ProductID", SqlDbType.Int,4)};
                parameters[0].Value = UserName;
                parameters[1].Value = ProductID;

                TimKen.DataEntity.ShoppingCart model = new TimKen.DataEntity.ShoppingCart();
                DataSet ds = DbHelperSQL.RunProcedure("UP_ShoppingCart_GetModel", parameters, "ds");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                    if (ds.Tables[0].Rows[0]["ProductID"].ToString() != "")
                    {
                        model.ProductID = int.Parse(ds.Tables[0].Rows[0]["ProductID"].ToString());
                    }
                    if (ds.Tables[0].Rows[0]["Number"].ToString() != "")
                    {
                        model.Number = decimal.Parse(ds.Tables[0].Rows[0]["Number"].ToString());
                    }
                    if (ds.Tables[0].Rows[0]["DateAdded"].ToString() != "")
                    {
                        model.DateAdded = DateTime.Parse(ds.Tables[0].Rows[0]["DateAdded"].ToString());
                    }
                    return model;
                }
                else
                {
                    return null;
                }
            }

            /// <summary>
            /// 获得数据列表
            /// </summary>
            public DataSet GetList(string strWhere)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select UserName,ProductID,Number,DateAdded ");
                strSql.Append(" FROM ShoppingCart ");
                if (strWhere.Trim() != "")
                {
                    strSql.Append(" where " + strWhere);
                }
                return DbHelperSQL.Query(strSql.ToString());
            }
            /// <summary>
            /// 分页获取数据列表
            /// </summary>
            public DataSet GetPageList(int PageSize, int PageIndex,string userName,int languageID)
            {
                SqlParameter[] parameters = {
                   
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@LanguageID",SqlDbType.Int)
                    };
                parameters[0].Value =  PageSize;
                parameters[1].Value =PageIndex;
                parameters[2].Value = userName;
                parameters[3].Value = languageID;
                return DbHelperSQL.RunProcedure("UP_ShoppingCart_GetPageList", parameters, "ds");
            }

            #endregion  成员方法
    
    }
}
